<?php

require_once 'facebook/exception.php';
require_once 'facebook/base.php';
require_once 'facebook/api.php';

define('AUTH_SERVICE', 'facebook');

require_once '../server.php';

$facebook = new Facebook_Api( array(
		'appId' => FACEBOOK_APPID,
		'secret' => FACEBOOK_SECRET
));

$redirect_url = AUTH_BASE_URL . '/facebook/connect.php?task=1';

if (isset($_REQUEST['code']))
{
	$access_token = $facebook -> getAccessToken();
	$_SESSION['facebook']['access_token'] = $access_token;
	$facebook -> setAccessToken($access_token);
	$data = $facebook -> api('/me');
    
    $result  = Mapper::fromFacebook($data);

	processCentralServiceResponseData($result);
}
else
{

	$params = array(
			'scope' => 'email,read_friendlists,offline_access',
			'redirect_uri' => $redirect_url
	);

	$loginUrl = $facebook -> getLoginUrl($params);

	header('location: ' . $loginUrl);

}
